package com.cuckoo.mall.thus.start;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;

import com.bgn.baseframe.base.BaseActivity;
import com.bgn.baseframe.utils.BasePackageUtil;
import com.bgn.baseframe.utils.PreferUtil;
import com.bgn.baseframe.utils.UiUtil;
import com.cuckoo.mall.R;

import java.util.ArrayList;
import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;


/**
 * 类描述：启动页的Activity
 * 创建人：wl
 * 创建时间：2018/4/8 15:43
 */
public class LaunchActivity extends BaseActivity {

    //启动页停留的时间
    private long stopTime = 2000;

    MyRunnable myRunnable = new MyRunnable();

    private final int BASE_PERMISSION_REQUEST = 127;

    private static String[] PERMISSIONS_BASE = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,};

    public static boolean is_normal_launch = false; //记录是否正常启动

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        is_normal_launch = true;
        super.onCreate(savedInstanceState);
//        if (getIntent() != null && getIntent().getExtras() != null&&getIntent().getExtras().containsKey("GCM")) {
//            Bundle bundle = getIntent().getExtras();
//            for (String key : bundle.keySet()) {
//                Logger.d(key + "___" + bundle.get(key));
//            }
//            Intent intent = new Intent(LaunchActivity.this, MainActivity.class);
//            startActivity(intent);
//            finish();
//            return;
//        }
        if (!this.isTaskRoot()) { // 判断当前activity是不是所在任务栈的根
            Intent intent = getIntent();
            if (intent != null) {
                String action = intent.getAction();
                if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(action)) {
                    finish();
                    return;
                }
            }
        }
        setIsLoadTitleBar(false);
        setContentView(R.layout.activity_launch);
        getStateBar().hide();
        requiresMainPermission();
    }

    public class MyRunnable implements Runnable {
        @Override
        public void run() {
            //第一次启动应用,跳转到启动页
            if (PreferUtil.getBoolean(PreferUtil.KEY_FIRST_START, true)) {
                Intent intent = new Intent(LaunchActivity.this, MainActivity.class);
                startActivity(intent);
            } else {
//                Intent intent = new Intent(LaunchActivity.this, MainActivity.class);
//                startActivity(intent);
                Intent intent = new Intent(LaunchActivity.this, MainActivity.class);
                startActivity(intent);
            }
            finish();
        }
    }

    @AfterPermissionGranted(BASE_PERMISSION_REQUEST)
    private void requiresMainPermission() {
        if (EasyPermissions.hasPermissions(this, PERMISSIONS_BASE)) {
            UiUtil.postDelayed(myRunnable, stopTime);
        } else {
            EasyPermissions.requestPermissions(this, UiUtil.getString(R.string.permisstion_tip), BASE_PERMISSION_REQUEST, PERMISSIONS_BASE);
        }

    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        super.onPermissionsGranted(requestCode, perms);
        if (requestCode == BASE_PERMISSION_REQUEST && perms.contains(Manifest.permission.READ_EXTERNAL_STORAGE)) {
            UiUtil.postDelayed(myRunnable, stopTime);
        }
    }


    @Override
    public void onPermissionsDenied(int requestCode, List<String> list) {
        if (requestCode == BASE_PERMISSION_REQUEST && list.contains(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            List<String> storages = new ArrayList<>();
            storages.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if (EasyPermissions.somePermissionPermanentlyDenied(this, storages)) {
                showGoSettingDilog();
            } else {
                requiresMainPermission();
            }
        }
    }

    private void showGoSettingDilog() {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(R.string.permanetly_denied_tip);
        builder.setMessage(R.string.permisstion_tip);
        builder.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Uri packageURI = Uri.parse("package:" + BasePackageUtil.getPackageName());
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
                startActivityForResult(intent, AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE);
            }
        });
        builder.create().show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
            requiresMainPermission();
        }
    }


}
